package com.ysd.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jspsmart.upload.Request;
import com.ysd.entity.Admin;

//过滤器
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request; // 将ServletRequest强转为HttpServletRequest
		
		HttpServletResponse res = (HttpServletResponse)response; //将ServletResponse强转为HttpServletResponse
		
		HttpSession session = req.getSession();  //创建session对象
		Admin admin = (Admin)session.getAttribute("admin"); // 从session中获取admin对象
		
		String url = req.getRequestURI();//获得访问的路径
		
		if(url.endsWith(".css") || url.endsWith(".js") || url.endsWith(".jpg")) { // 如果访问静态资源也放行
			chain.doFilter(req, res); 
		} else if(url.indexOf("login.html")>0) { //访问的是登陆页面  放行
			chain.doFilter(req, res);
		}else { // 访问的不是登陆页面
			if(admin != null) { //登陆过
				chain.doFilter(req, res); // 登陆过了  放行
			}else { // 没登陆过
				res.sendRedirect("/guofengmeizhuang/admin/login.html"); //重定向
			}
		}
		
		
	
	
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	
}
